package cn.wolfcode.service.impl;

import cn.wolfcode.pojo.User;
import cn.wolfcode.repository.UserRepository;
import cn.wolfcode.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @Description: 用户业务接口实现类
 * @Author: 伍家钰
 * @Date: 2020/8/3 10:39
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserRepository userRepository;

    @Override
    public void save(User user) {
        userRepository.insert(user);
    }

    @Override
    public void delete(String id) {
        userRepository.deleteById(id);
    }

    @Override
    public void update(User user) {
        userRepository.save(user);
    }

    @Override
    public User selectOne(String id) {
        Optional<User> user = userRepository.findById(id);
        return user.orElse(null);
    }

    @Override
    public List<User> selectAll() {
        List<User> users = userRepository.findAll();
        return users;
    }

    @Override
    public User findByName(String name) {
        User user = userRepository.findByName(name);
        return user;
    }
}
